package com.example.smartcity1.my;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity1.R;
import com.example.smartcity1.bean.MsgCodeBean;
import com.example.smartcity1.bean.MyFeedbackPostBean;
import com.example.smartcity1.network.RetrofitClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyFeedbackActivity extends AppCompatActivity {

    private EditText myFeedbackEdit;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_feedback);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle("意见反馈");
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        initView();
        SharedPreferences user = getSharedPreferences("user",MODE_PRIVATE);
        button.setOnClickListener(v -> {
            String text = myFeedbackEdit.getText().toString();
            if (!text.equals("")) {
                MyFeedbackPostBean data = new MyFeedbackPostBean(text, user.getString("userId",""));
                RetrofitClient.appService().getMyFeedbackData(user.getString("token",""),data).enqueue(new Callback<MsgCodeBean>() {
                    @Override
                    public void onResponse(Call<MsgCodeBean> call, Response<MsgCodeBean> response) {
                        MsgCodeBean bean = response.body();
                        if (bean != null && bean.getCode() == 200) {
                            Toast.makeText(MyFeedbackActivity.this, "已提交", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(MyFeedbackActivity.this, bean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<MsgCodeBean> call, Throwable t) {

                    }
                });
            } else {
                Toast.makeText(this, "请输入意见", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }

    private void initView() {
        myFeedbackEdit = findViewById(R.id.my_feedback_edit);
        button = findViewById(R.id.button);
    }
}